/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.customer;

import java.io.Serializable;
import java.io.PrintWriter;

import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import src.domain.customer.Customer;
import src.services.CRUEDService;
import src.services.customer.CustomerModel;

/**
 *
 * @author jasuke
 */
@ManagedBean
@SessionScoped
public final class SearchCustomer implements Serializable{

    /**
     * Creates a new instance of SearchCustomer
     */
    
    private LazyDataModel<Customer> lazyModel;  
    private Customer selectedData;  
    private int pageSize = 10;
    private Integer customerId;
    private String cif, nama;
        
    public SearchCustomer() {
        selectedData = null;
        cif = null;
        nama = null;
        lazyModel = null;
    }

    public Integer getCustomerId() {
        return customerId;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    
    public Customer getSelectedData() {
        return selectedData;
    }

    public void setSelectedData(Customer selectedData) {
        this.selectedData = selectedData;
    }

    public String getCif() {
        return cif;
    }

    public void setCif(String cif) {
        this.cif = cif;
    }

    public String getNama() {
        return nama;
    }

    public void setNama(String nama) {
        this.nama = nama;
    }
    
    public void handleClose() {
        selectedData = null;
        cif = null;
        nama = null;
        lazyModel = null;
    }
           
    public void loadData() {
        lazyModel = new LazyDataModel<Customer>() {

            @Override
            public List<Customer> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String,String> filters) { 
                                                
                int start = first;
                int end = first + pageSize;

                filters.put("cif", cif);
                filters.put("customerName", nama);
                filters.put("approvalUid", "notnull");

                List<Customer> list = (List) new CRUEDService().loadLazy("Customer", start, end, sortField, sortOrder, filters);

                Integer count = new CRUEDService().countLazy("Customer", filters);
                lazyModel.setRowCount(count);
                return list;
            }
        };
    }
                                   
    public LazyDataModel<Customer> getLazyModel () {
        return lazyModel;
    }
    
    public void onrowselect(SelectEvent ev) {
    }
}
